<?php

namespace App\GraphQL\Queries\Order;

use App\Models\Order;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Support\Carbon;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use App\Models\GroupMember;

class QueryList
{
    /**
     * Return a value for the field.
     *
     * @param  null  $rootValue Usually contains the result returned from the parent field. In this case, it is always `null`.
     * @param  mixed[]  $args The arguments that were passed into the field.
     * @param  \Nuwave\Lighthouse\Support\Contracts\GraphQLContext  $context Arbitrary data that is shared between all fields of a single query.
     * @param  \GraphQL\Type\Definition\ResolveInfo  $resolveInfo Information about the query itself, such as the execution state, the field name, path to the field from the root, and more.
     * @return mixed
     */
    public function __invoke($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
    {
        $query = Order::query();
        $query->latest();
        $page = $args['input']['page'];
        $keywords = $args['input']['keywords'];
        $style = $args['input']['device_style'];
        $status = $args['input']['status'];
        $date = $args['input']['date'];
        $channel = $args['input']['channel'];
        $authUser = auth()->user();
        if($style){
            $query->where('device_style',$style);
        }
        if($status){
            $query->where('status',$status);
        }
        if($date){
            $query->where('created_at','<=',(new Carbon($date))->format('Y-m-d 23:59:59'));
        }
        $mode = request()->header('mode');
        if($mode === 'wechats'){
            $query->where('member_id',$authUser->id)->where('channel','WECHAT');
        }elseif($mode === 'alipays'){
            $query->where('member_id',$authUser->id)->where('channel','ALIPAY');
        }else{
            if($authUser->role_code !== 'ADMIN'){
                $groups = GroupMember::where('role','MERCHANT')->where('user_id',$authUser->id)->pluck('group_id');
                if($groups->count() > 0){
                    $query->whereIn('device_group_id',$groups->toArray());
                } else{
                    $query->where('member_id',0);
                }
            }
            if ($channel) {
                $query->where('channel','ALIPAY');
            }
        }
        
        if($keywords){
            $query->where(function($query) use(&$keywords){
                $query->where('device_group_id','LIKE',"%{$keywords}%")->orWhereIn('device_id','LIKE',"%{$keywords}%");
            });
        }
        $pageInfo = $query->paginate($args['input']['limit'],['*'],'page',$page);
        return [
            'data'=>$pageInfo->items(),
            'paginatorInfo'=>[
                'count'=> $pageInfo->count(),
                'currentPage'=>$pageInfo->currentPage(),
                'firstItem'=>$pageInfo->firstItem(),
                'hasMorePages'=>$pageInfo->hasMorePages(),
                'lastItem'=>$pageInfo->lastItem(),
                'lastPage'=>$pageInfo->lastPage(),
                'perPage'=>$pageInfo->perPage(),
                'total'=>$pageInfo->total()
            ]
        ];
    }
}
